/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Public License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/opensource/licenses.mspx#Ms-PL

 ****************************************************************************************/
using System;

namespace Microsoft.CSA.FeedSync.FeedSyncLibrary
{
    public class RSSFeed : Microsoft.CSA.FeedSync.FeedSyncLibrary.FeedSyncFeed<Microsoft.CSA.FeedSync.FeedSyncLibrary.RSSFeedSyncItem>
    {
        private string m_Title;
        private string m_Description;
        private string m_Link;

        internal RSSFeed()
        {
        }

        public RSSFeed(string i_Title, string i_Description, string i_Link)
        {
            if (System.String.IsNullOrEmpty(i_Title))
                throw new System.ArgumentException("Invalid value for i_Title!");

            string FeedContents = System.String.Format
                (
                Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_FEED_TEMPLATE,
                i_Title,
                System.String.IsNullOrEmpty(i_Description) ? "" : i_Description,
                System.String.IsNullOrEmpty(i_Link) ? "" : i_Link
                );

            System.Xml.XmlDocument XmlDocument = new System.Xml.XmlDocument();
            XmlDocument.LoadXml(FeedContents);

            System.Xml.XmlNamespaceManager XmlNamespaceManager = new System.Xml.XmlNamespaceManager(XmlDocument.NameTable);

            System.Xml.XmlQualifiedName XmlQualifiedName = new System.Xml.XmlQualifiedName("channel");
            this.Initialize
                (
                XmlDocument,
                XmlNamespaceManager,
                XmlQualifiedName
                );
        }

        public RSSFeed(System.Xml.XmlDocument i_XmlDocument)
        {
            if (i_XmlDocument == null)
                throw new System.ArgumentException("i_XmlDocument must be non-null!");

            System.Xml.XmlNamespaceManager XmlNamespaceManager = new System.Xml.XmlNamespaceManager(i_XmlDocument.NameTable);

            System.Xml.XmlQualifiedName ItemContainerXmlQualifiedName = new System.Xml.XmlQualifiedName(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_CHANNEL_ELEMENT);
            this.Initialize
                (
                i_XmlDocument,
                XmlNamespaceManager,
                ItemContainerXmlQualifiedName
                );
        }

        public RSSFeed(System.Xml.XmlDocument i_XmlDocument, System.Xml.XmlNamespaceManager i_XmlNamespaceManager)
        {
            System.Xml.XmlQualifiedName ItemContainerXmlQualifiedName = new System.Xml.XmlQualifiedName(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_CHANNEL_ELEMENT);
            this.Initialize
                (
                i_XmlDocument,
                i_XmlNamespaceManager,
                ItemContainerXmlQualifiedName
                );
        }

        private new void Initialize(System.Xml.XmlDocument i_XmlDocument, System.Xml.XmlNamespaceManager i_XmlNamespaceManager, System.Xml.XmlQualifiedName i_ItemContainerXmlQualifiedName)
        {
            base.Initialize
                (
                i_XmlDocument,
                i_XmlNamespaceManager,
                i_ItemContainerXmlQualifiedName
                );

            //  Get title
            System.Xml.XmlNode TitleXmlNode = m_ItemContainerXmlElement.SelectSingleNode(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_TITLE_ELEMENT_NAME);
            if (TitleXmlNode != null)
                m_Title = TitleXmlNode.InnerText;

            //  Get description
            System.Xml.XmlNode DescriptionXmlNode = m_ItemContainerXmlElement.SelectSingleNode(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_DESCRIPTION_ELEMENT_NAME);
            if (DescriptionXmlNode != null)
                m_Description = DescriptionXmlNode.InnerText;

            //  Get link
            System.Xml.XmlNode LinkXmlNode = m_ItemContainerXmlElement.SelectSingleNode(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_LINK_ELEMENT_NAME);
            if (LinkXmlNode != null)
                m_Link = LinkXmlNode.InnerText;
        }

        public string Title
        {
            get
            {
                return m_Title;
            }
        }

        public string Description
        {
            get
            {
                return m_Description;
            }
        }

        public string Link
        {
            get
            {
                return m_Link;
            }
        }

        public System.Xml.XmlElement ChannelXmlElement
        {
            get
            {
                return m_ItemContainerXmlElement;
            }
        }

        protected override void PopulateItems()
        {
            //  Iterate items
            System.Xml.XmlNodeList FeedSyncItemXmlNodeList = m_ItemContainerXmlElement.SelectNodes(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_ITEM_ELEMENT_NAME);

            foreach (System.Xml.XmlElement FeedSyncItemXmlElement in FeedSyncItemXmlNodeList)
            {
                Microsoft.CSA.FeedSync.FeedSyncLibrary.RSSFeedSyncItem RSSFeedSyncItem = new RSSFeedSyncItem
                    (
                    FeedSyncItemXmlElement,
                    m_XmlNamespaceManager
                    );

                m_FeedSyncItemSortedList[RSSFeedSyncItem.Sync.ID] = RSSFeedSyncItem;
            }
        }
    }
}
